package ar.com.personalTravellers.rowProcessors;

import ar.com.personalTravellers.beans.Continent;
import ar.com.personalTravellers.beans.Country;
import com.angel.base.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.impl.HeaderRowFile;
import com.angel.io.type.rows.interfaces.ImportRowFile;

/**
 *
 * @author William
 */
public class CountryRowProcessorCommand implements ImportRowProcessorCommand {

    public static final String NAME_COLUMN = "name";
    public static final String LOCALE_COLUMN = "locale";
    public static final String CONTINENT_COLUMN = "continent";
    public static final String ABBREVIATION_COLUMN = "abbreviation";

    public void checkRowData(ImportRowFile importRowFile, HeaderRowFile headerRowFile) throws InvalidRowDataException {
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(NAME_COLUMN));
		String locale = importRowFile.getStringValue(headerRowFile.getColumnPosition(LOCALE_COLUMN));
        String continent = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTINENT_COLUMN)).toUpperCase().replaceAll(" ", "_");
        String abbreviation = importRowFile.getStringValue(headerRowFile.getColumnPosition(ABBREVIATION_COLUMN));

        Boolean areAllNotEmpty = StringHelper.areAllNotEmpty(name, locale, continent, abbreviation);
        if( !areAllNotEmpty || Continent.valueOf(continent) == null){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"name: [" + name + "] - " +
                    "locale: [" + locale + "] - " +
                    "abbreviation: [" + abbreviation + "] - " +
                    "continent: [" + continent  + "].");
		}
    }

    public Object processRow(ImportRowFile importRowFile, HeaderRowFile headerRowFile, ParametersService parametersService) {
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(NAME_COLUMN));
		String locale = importRowFile.getStringValue(headerRowFile.getColumnPosition(LOCALE_COLUMN));
        String continent = importRowFile.getStringValue(headerRowFile.getColumnPosition(CONTINENT_COLUMN)).toUpperCase().replaceAll(" ", "_");
        String abbreviation = importRowFile.getStringValue(headerRowFile.getColumnPosition(ABBREVIATION_COLUMN));

        Country country = new Country();
        country.setContinent(Continent.valueOf(continent));
        country.setAbbreviation(abbreviation);
        country.setLocale(locale);
        country.setName(name);

        return country;
    }

    public String[] getHeaderColumnsNames() {
        return new String[] {NAME_COLUMN, LOCALE_COLUMN, CONTINENT_COLUMN, ABBREVIATION_COLUMN};
    }
}